package com.gethistorics.components;

import java.awt.AlphaComposite;
import java.awt.BorderLayout;
import java.awt.Canvas;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.GradientPaint;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.GraphicsDevice;
import java.awt.GraphicsEnvironment;
import java.awt.Image;
import java.awt.LayoutManager;
import java.awt.Paint;
import java.awt.Rectangle;
import java.awt.RenderingHints;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseWheelEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.awt.geom.AffineTransform;
import java.awt.geom.Rectangle2D;
import java.awt.image.AffineTransformOp;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

import javax.imageio.ImageIO;
import javax.swing.BorderFactory;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;

public class HistoryBrowser extends JFrame implements ActionListener {
   private static final Color BG_COLOR = Color.BLACK;
   private static final String CLOSE_ACTION = "Close";
   private static final String CLOSE_ICON = "exit.png";//"close.png";
   private static final String CLOSE_OVER_ICON = "exit_over.png";//"close.png";
   
   private JGradPanel topControlPanel, bottomControlPanel;
   private ImageTileSequence canvasComponent;
   private JButton closeButton;

   private static final Color controlPanelColor1 = new Color(0.5f, 0.5f, 0.5f, 0.7f);
   private static final Color controlPanelColor2 = new Color(0.1f, 0.1f, 0.1f, 0.7f);
   
   
   public HistoryBrowser(){
      createGUI();
      addWindowListener(new WindowAdapter() {
         public void windowClosing(WindowEvent e) {
            close();
         }
      });
   }
   
   private void createGUI(){
      try {
         UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
      } catch (Exception e) {}

      setUndecorated(true);
      setExtendedState(MAXIMIZED_BOTH);
      
      GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
      GraphicsDevice primaryScreen = ge.getDefaultScreenDevice();
      if(primaryScreen.isFullScreenSupported()){
         primaryScreen.setFullScreenWindow(this);
      }
      
      getContentPane().setLayout(null);
      
      topControlPanel = new JGradPanel(new FlowLayout(FlowLayout.TRAILING),
            controlPanelColor1, 
            controlPanelColor2,
            JGradPanel.DIR_DOWN);
      topControlPanel.setBounds(0, 0, getWidth(), 45);
      
      closeButton = new JButton();
      ImageIcon closeIcon = Utils.getIcon(CLOSE_ICON);
      Dimension closeIconSize = new Dimension(closeIcon.getIconWidth() + 8, closeIcon.getIconHeight() + 8);
      closeButton.setBorder(BorderFactory.createLineBorder(Color.white));
      closeButton.setSize(closeIconSize);
      closeButton.setPreferredSize(closeIconSize);
      closeButton.setMaximumSize(closeIconSize);
      closeButton.setIcon(closeIcon);
      closeButton.setActionCommand(CLOSE_ACTION);
      closeButton.addActionListener(this);
      closeButton.setOpaque(false);
      closeButton.setContentAreaFilled(false);
      closeButton.setSelected(false);
      closeButton.setFocusPainted(false);
      topControlPanel.add(closeButton);
      getContentPane().add(topControlPanel);    
      
      bottomControlPanel = new JGradPanel(new FlowLayout(FlowLayout.TRAILING),
            controlPanelColor1, 
            controlPanelColor2,
            JGradPanel.DIR_UP);
      bottomControlPanel.setBounds(0, getHeight() - 45, getWidth(), 45);
      getContentPane().add(bottomControlPanel);
      
      canvasComponent = new ImageTileSequence(500, 500);
      canvasComponent.setBounds(0, 0, getWidth(), getHeight());
      getContentPane().add(canvasComponent);
   }

   @Override
   public void actionPerformed(ActionEvent e) {
      String action = e.getActionCommand();
      if (action.equals(CLOSE_ACTION)) {
         close();
      }
   }

   private void close(){
      dispose();
      System.exit(0);
   }
   
   public String getPath(){
      return System.getProperty("user.dir");
   }
   /**
    * @param args
    */
   public static void main(String[] args) {
      // TODO Auto-generated method stub
      SwingUtilities.invokeLater(new Runnable(){
         @Override
         public void run() {
            new HistoryBrowser().setVisible(true);
         }
         
      });
   }

}
class JGradPanel extends JPanel {
   public static final int DIR_UP = 1;
   public static final int DIR_DOWN = 2;
   public static final int DIR_LEFT = 3;
   public static final int DIR_RIGHT = 4;
   Color color1;
   Color color2;
   int direction;
   
   public JGradPanel(Color color1, Color color2, int direction) {
      this(null, color1, color2, direction);
   }
   
   public JGradPanel(LayoutManager layout, Color color1, Color color2, int direction) {
      super(layout);
      this.color1 = color1; 
      this.color2 = color2;
      this.direction = direction;
   }
   
   public boolean isOpaque() {
      return true;
   }
        
   public void paintComponent(Graphics _g) {
      Graphics2D g = (Graphics2D)_g;
       
      Rectangle bounds = getBounds();
      
      g.setPaint(color2);
      g.fillRect(0, 0, bounds.width, bounds.height);
      
      //g.setPaint(color2);
      //g.fillRect(0, bounds.height/2, bounds.width, bounds.height);
      
      //g.setPaint(Color.white);
      //g.drawLine(0, bounds.height - 1, bounds.width, bounds.height-1);
      
      //g.setPaint(color1);
      //g.drawLine(0, bounds.height-1, bounds.width, bounds.height-1);
      
      int xStart = direction==DIR_LEFT?bounds.width:direction==DIR_RIGHT?0:bounds.width/2;
      int yStart = direction==DIR_UP?0:direction==DIR_DOWN?bounds.height:bounds.height/2;
      int xEnd = (direction==DIR_LEFT || direction==DIR_RIGHT)?bounds.width/2:xStart;
      int yEnd = (direction==DIR_UP || direction==DIR_DOWN)?bounds.height/2:yStart;
      
      // Set Paint for filling Shape
      Paint gradientPaint = new GradientPaint(
            xStart,
            yStart,
            color1, 
            xEnd, 
            yEnd, 
            color2);
      g.setPaint(gradientPaint);
      g.fillRect(0, 0, bounds.width, bounds.height);
   }
}

